/**
 * Copyright (c) 2010 sonntagsfarben.de
 * Copyright (c) 2006 - 2008  Eric J. Feminella  <eric@ericfeminella.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

package de.sonntagsfarben.collections
{
	import flash.utils.Dictionary;

	/**
	 * @author Soenke Kluth <sonntagsfarben.de>
	 * @copyright (c) 2010 Soenke Kluth.
	 * Copyright (c) 2006 - 2008  Eric J. Feminella  <eric@ericfeminella.com>
	 */

	public final class HashMap
	{
		private var _map:Dictionary;
		private var _useWeakReferences:Boolean;


		public function HashMap(useWeakReferences:Boolean = true)
		{
			_useWeakReferences = useWeakReferences;
			_map = new Dictionary(_useWeakReferences);
		}


		public function put(key:*, value:*):void
		{
			_map[key] = value;
		}


		public function remove(key:*):void
		{
			delete _map[key];
		}


		public function putAll(map:HashMap):void
		{
			for (var key:* in map)
			{
				_map[key] = map.getValue(key);
			}
		}


		public function containsKey(key:*):Boolean
		{
			return _map[key] != null;
		}


		public function containsValue(value:*):Boolean
		{
			for (var key:* in _map)
			{
				if (_map[key] == value)
				{
					return true;
				}
			}
			return false;
		}


		public function getKey(value:*):String
		{
			for (var key:* in _map)
			{
				if (_map[key] == value)
				{
					return key;
				}
			}
			return null;
		}


		public function get keys():Array
		{
			var keys:Array = [];

			for (var key:* in _map)
			{
				keys.push(key);
			}
			return keys;
		}


		public function getValue(key:*):*
		{
			return _map[key];
		}


		public function get values():Array
		{
			var values:Array = [];

			for (var key:* in _map)
			{
				values.push(_map[key]);
			}
			return values;
		}


		public function size():int
		{
			var length:int = 0;

			for (var key:* in _map)
			{
				length++;
			}
			return length;
		}


		public function isEmpty():Boolean
		{
			for (var key:* in _map)
			{
				return false;
			}
			return true;
		}


		public function reset():void
		{
			for (var key:* in _map)
			{
				_map[key] = null;
			}
		}


		public function resetAllExcept(keyId:*):void
		{
			for (var key:* in _map)
			{
				if (key != keyId)
				{
					_map[key] = null;
				}
			}
		}


		public function clear():void
		{
			for (var key:* in _map)
			{
				remove(key);
			}
		}


		public function clearAllExcept(keyId:*):void
		{
			for (var key:* in _map)
			{
				if (key != keyId)
				{
					remove(key);
				}
			}
		}

		public function clone():HashMap
		{
			var hashMap:HashMap = new HashMap(_useWeakReferences);
			hashMap.putAll(this);
			return hashMap;
		}
		
		
		
		public function get iterator():IIterator
		{
			return new HashMapIterator(this);
		}
	}
}
